package com.xxzb.pack0722.组装.dao;

import com.xxzb.bean.Dept;
import com.xxzb.db.DBManager;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeptDAO {
    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    //根据部门id获取部门信息
    public Dept findByNo(int deptNo) {
        try {
            conn = DBManager.getConnection();
            String sql = "select * from gaofeng2207.dept where deptNo=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, deptNo);
            rs = ps.executeQuery();
            if (rs.next()) {
    //            int deptNo=rs.getInt(1);
                String dname = rs.getString(2);
                String loc = rs.getString(3);
                Dept dept = new Dept(deptNo, dname, loc);
                return dept;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //全表查询获取dept表的全部信息
    public List<Dept> find() throws Exception {
        List<Dept> list=new ArrayList<>();
        String sql="select * from gaofeng2207.dept";
        conn= DBManager.getConnection();
        ps=conn.prepareStatement(sql);
         rs = ps.executeQuery();
         while(rs.next()){
             int deptNo=rs.getInt(1);
             String dname=rs.getString(2);
             String loc=rs.getString(3);
             Dept dept=new Dept(deptNo,dname,loc);
             list.add(dept);
         }
         return list;


    }












}
